<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org/">
<head>
    <meta charset="UTF-8">
    <title>天天生鲜-商品详情</title>
    <link rel="stylesheet" type="text/css" th:href="@{/css/reset.css}">
    <link rel="stylesheet" type="text/css" th:href="@{/css/main.css}">

</head>
<body>

<div th:replace="top::top"></div>

<div class="goods_detail_con clearfix">
    <div class="goods_detail_pic fl"><img  width="350px" height="350px" th:src="${product.mainImageFullUrl}"></div>

    <div class="goods_detail_list fr">
        <h3 th:text="${product.name}"></h3>
        <p th:text="${product.subtitle}"></p>
        <div class="prize_bar">
            <span class="show_pirze">¥<em th:text="${product.price}"></em></span>
            <span class="show_unit">单  位：1</span>
        </div>
        <div class="goods_num clearfix">
            <div class="num_name fl">数 量：1</div>
            <div class="num_add fl">
                <input id="quantity" type="text" class="num_show fl" value="1">
                <a href="javascript:;" class="add fr">+</a>
                <a href="javascript:;" class="minus fr">-</a>
            </div>
        </div>
        <div class="total">单价：<em th:text="${product.price}">元</em></div>
        <div class="operate_btn">
            <a href="javascript:;" class="buy_btn">立即购买</a>
            <a href="javascript:;" class="add_cart" id="add_cart">加入购物车</a>
        </div>
    </div>
</div>

<div class="main_wrap clearfix">
    <div class="l_wrap fl clearfix">
        <div class="new_goods">
            <h3>新品推荐</h3>
            <ul>
                <li>
                    <a href="#"><img th:src="@{/images/goods/goods001.jpg}"></a>
                    <h4><a href="#">进口柠檬</a></h4>
                    <div class="prize">￥3.90</div>
                </li>
                <li>
                    <a href="#"><img th:src="@{/images/goods/goods002.jpg}"></a>
                    <h4><a href="#">玫瑰香葡萄</a></h4>
                    <div class="prize">￥16.80</div>
                </li>
            </ul>
        </div>
    </div>

    <div class="r_wrap fr clearfix">
        <ul class="detail_tab clearfix">
            <li class="active">商品介绍</li>
            <li>评论</li>
        </ul>

        <div class="tab_content">
            <dl>
                <dt>商品详情：</dt>
                <dd th:utext="${product.detail}"></dd>
            </dl>
        </div>

    </div>
</div>

<div class="footer">
    <div class="foot_link">
        <a href="#">关于我们</a>
        <span>|</span>
        <a href="#">联系我们</a>
        <span>|</span>
        <a href="#">招聘人才</a>
        <span>|</span>
        <a href="#">友情链接</a>
    </div>
    <p>CopyRight ©2023 Zer天天生鲜信息技术有限公司 All Rights Reserved</p>
    <p>电话：17658159724    京ICP备16888888号</p>
</div>
<div class="add_jump"></div>

<div id="loginForm" style="display: none" class="login_form fr">
    <div class="login_title clearfix">
        <h1>用户登录</h1>
        <a href="#">立即注册</a>
    </div>
    <div class="form_input">
        <form id="formId">
            <input type="text" name="username" class="name_input" placeholder="请输入用户名">
            <div class="user_error">输入错误</div>
            <input type="password" name="password" class="pass_input" placeholder="请输入密码">
            <div class="pwd_error">输入错误</div>
            <div class="more_input clearfix">
                <input type="checkbox" name="">
                <label>记住用户名</label>
                <a href="#">忘记密码</a>
            </div>
            <input type="button" onclick="submitForm()" name="" value="登录" class="input_submit">
        </form>
    </div>
</div>

<script type="text/javascript" th:src="@{/js/jquery-2.1.4.js}"></script>
<script type="text/javascript" th:src="@{/js/layer/layer.js}"></script>
<script type="text/javascript" th:src="@{/js/mylayer.js}"></script>
<script type="text/javascript">
    $('#add_cart').click(function(){
        //加入购物车之前首先判断有没有登录
        //如果没有登录首先完成登录
        //如果已经登录，就将商品信息插入到cart购物车表里面，然后跳转到购物车列表界面
        $.post(
            '/user/checkUserLogin',
            function(jsonResult) {
                if (jsonResult.ok) {
                    //已经登录直接加入购物车
                    insertToCart();
                } else {
                    layer.open({
                        type: 1,
                        title: '登录',
                        area: ['370px', '480px'],
                        content: $('#loginForm')
                    });
                }
            },
            'json'
        );
    })

    function submitForm() {
        $.post(
            '/user/login',
            $('#formId').serialize(),
            function(jsonResult) {
                if (jsonResult.ok) {
                    insertToCart();
                } else {
                    mylayer.errorMsg(jsonResult.msg);
                }
            },
            'json'
        );
    }

    function insertToCart() {
        $.post(
            '/cart/add',
            {'productId': [[${product.id}]], 'quantity':$('#quantity').val()},
            function(jsonResult) {
                if (jsonResult.ok) {
                    mylayer.okUrl(jsonResult.msg, '/cart/getCartListPage')
                } else {
                    mylayer.errorMsg(jsonResult.msg);
                }
            },
            'json'
        );
    }

    var $add_x = $('#add_cart').offset().top;
    var $add_y = $('#add_cart').offset().left;

    var $to_x = $('#show_count').offset().top;
    var $to_y = $('#show_count').offset().left;

    $(".add_jump").css({'left':$add_y+80,'top':$add_x+10,'display':'block'})
    $('#add_cart').click(function(){
        $(".add_jump").stop().animate({
                'left': $to_y+7,
                'top': $to_x+7},
            "fast", function() {
                $(".add_jump").fadeOut('fast',function(){
                    $('#show_count').html(2);
                });

            });
    })
</script>

</div>
<p>CopyRight © 2023 Qing天天生鲜信息技术有限公司 All Rights Reserved</p>
<p>电话：17658159724    鲁ICP备23030588888号</p>
</div>

</body>
</html>